//
//  VRSettingViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/15.
//

import UIKit

class VRSettingViewController: VRBaseViewController, SAAnimationProtocol {

    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(20.font_bold)
        .alignment(.center).text("Setting").end()
    private let logotButton          = c_make_ex(UIButton.self)
        .text("Sign Out").corner(25).bgColor("#FFFC00".toColor.alpha(0.1))
        .color("#FFFC00".toColor.alpha(0.3)).font(18.font_bold).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 60
        tableView.register(VRSettingCell.self)
        return tableView
    }()
    private let itemInfos: [VRProfileModel] = [.black,.terms,.privacy,.delete,.clear,.version]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
            logotButton
        }
        
        backButton.top(.statusHeight + 2).leading(5).size(40)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).height(360)
        logotButton.leading(15).width(145).height(50)
        logotButton.Top == tableView.Bottom + 20
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }

}

extension VRSettingViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        itemInfos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(VRSettingCell.self, indexPath: indexPath)
        cell.itemType = itemInfos[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        switch itemInfos[indexPath.row]{
        case .black:
            
            break
        case .delete:
            let deleteV = VRDeleteAccountView()
            deleteV.deleteClosure = {
                
            }
            saAnimate.animation(show: deleteV)
            break
        case .terms:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Terms of use"
//            webVC.webUrl = URLTool.technicalSupportUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
            break
        case .privacy:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Privacy Policy"
//            webVC.webUrl = URLTool.privacyPolicyUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
            break
        case .clear:
//            let clearView = LearningClearCacheView()
//            clearView.didCleanCompletion = { [weak self] in
//                self?.tableView.reloadData()
//            }
//            saAnimate.animation(show: clearView)
            break
        default:
            return
        }
    }
}

class VRSettingCell: UITableViewCell {

    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).end()
    private let rightImage          = c_make_ex(UIImageView.self)
        .image("Setting_go").end()
    private let subLabel          = c_make_ex(UILabel.self)
        .color(.white.alpha(0.4)).font(14.font_medium).alignment(.right).end()

    var itemType: VRProfileModel = .none {
        didSet {
            titleLabel.text = itemType.title
            if itemType.isShowRightImageView {
                rightImage.isHidden = false
                subLabel.isHidden = true
            }else{
                rightImage.isHidden = true
                subLabel.isHidden = false
                subLabel.text = itemType.content
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            titleLabel
            rightImage
            subLabel
        }
        
        titleLabel.leading(15).centerVertically()
        rightImage.trailing(15).size(40).centerVertically()
        subLabel.trailing(15).centerVertically()
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
